package pers.qianyu.month_202101.date_20210110;

/**
 * 231. 2的幂
 * https://leetcode-cn.com/problems/power-of-two/submissions/
 *
 * @author mizzle rain
 * @date 2021-01-10 11:12
 */
public class IsPowerOfTwo {
    public boolean isPowerOfTwo(int n) {
        if (n <= 0) return false;
        // x & (-x) 表示 x 只保留最右边的一个 1
        return (n & (-n)) == n;
    }

    public boolean isPowerOfTwo2(int n) {
        if (n <= 0) return false;
        return ((1 << 30) % n) == 0;
    }

    public boolean isPowerOfTwo3(int n) {
        if (n <= 0) return false;
        return (n & (n - 1)) == 0;
    }
}
